package com.walterweimod.auxiliary_attributes.util;

import com.google.common.collect.Lists;
import com.walterweimod.auxiliary_attributes.mobeffect.ModMobEffects;
import net.minecraft.world.effect.MobEffect;
import net.minecraft.world.effect.MobEffectCategory;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.effect.MobEffects;
import net.minecraft.world.entity.LivingEntity;
import org.checkerframework.checker.nullness.qual.Nullable;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
import java.util.Objects;
import java.util.function.Consumer;

public class ModMobEffectUtils {
    private ModMobEffectUtils(){}

    public static void addEffect(LivingEntity livingEntity,
                                 MobEffect mobEffect,
                                 int duration,
                                 int amplifier){
        if (Objects.nonNull(livingEntity)
                && Objects.nonNull(mobEffect)
                && !livingEntity.level.isClientSide()){
            livingEntity.addEffect(new MobEffectInstance(mobEffect, duration, amplifier));
        }
    }

    public static int getMaxMobEffectAmplifier(MobEffect mobEffect){
        if (Objects.nonNull(mobEffect)){
            if (mobEffect == MobEffects.DAMAGE_RESISTANCE) return 3;
            if (mobEffect == MobEffects.JUMP) return 9;
            if (mobEffect == MobEffects.MOVEMENT_SPEED) return 19;
            if (mobEffect == MobEffects.REGENERATION) return 5;
        }

        return Short.MAX_VALUE;
    }

    public static void removeHarmfulMobEffects(LivingEntity livingEntity){
        if (Objects.nonNull(livingEntity) && !livingEntity.level.isClientSide()){
            Collection<MobEffectInstance> effectInstances = livingEntity.getActiveEffects();
            ArrayList<MobEffect> harmfulEffects = Lists.newArrayList();

            effectInstances.forEach(mobEffectInstance -> {
                MobEffect mobEffect = mobEffectInstance.getEffect();

                if (mobEffect.getCategory() == MobEffectCategory.HARMFUL){
                    harmfulEffects.add(mobEffect);
                }
            });

            harmfulEffects.forEach(livingEntity::removeEffect);
        }
    }
}
